//
//  CMBDPageTitleView.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/7.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

protocol CMDBPageTitleViewDelegate : class {
    func pageTitleView(titleView : CMDBPageTitleView,selectedIndex index : Int)
}


class CMDBPageTitleView: CMDBBaseView {
    
    let lineH : CGFloat        = 3
    
    var tagViewW : CGFloat     = 0
    
    var currentIndex : Int     = 0
    
    var scrolLineY : CGFloat   = 0
    
    var titleFont : UIFont     = CMDB_15_FONT
        
    var titles : [String]
    
    weak var delegate : CMDBPageTitleViewDelegate?
    
    lazy var titleLabels : [UILabel]   = [UILabel]()
    
    lazy var scrollView : UIScrollView = {
        
        let scrollView = UIScrollView()
        
        scrollView.showsVerticalScrollIndicator   = false
        
        scrollView.showsHorizontalScrollIndicator = false
        
        scrollView.scrollsToTop                   = false
        
        scrollView.bounces                        = false
        
        return scrollView
        
    }()
    
    lazy var scrollLine : UIView = {
    
        let scrollLine = UIView()
        
        scrollLine.backgroundColor = UIColor.Hex6(labelBlackColor, alpha: labelFirBlackAlpha)
        
        return scrollLine
    }()
    
    // MARK: 自定义构造函数
    init(frame: CGRect, titles : [String]) {
        self.titles = titles
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK:- 设置UI界面
extension CMDBPageTitleView{
    
    func setupUI(){
        
        //1.add scrollview
        addSubview(scrollView)
        
        scrollView.frame = self.bounds
        
        //添加title对应的Label
        setupTitleLabels()
        
        setupBottomLineAndScrollLine()
        
    }
    
    func setupTitleLabels(){
        
        let labelH : CGFloat = frame.height
        
        let labelY : CGFloat = 0
        
        tagViewW             = CMDB_Screen_Width / CGFloat(titles.count)
        
        for (index,title) in titles.enumerate() {
            let label           = UILabel()
            
            let tagView         = UIView()
            
            tagView.tag         = index
            
            label.text          = title
            
            label.font          = CMDB_15_FONT
            
            label.textColor     = word54Color
            
            label.textAlignment = .Center
            
            let labelX          = CGFloat(index) * tagViewW
            
            tagView.frame       = CGRect(x: labelX, y: labelY, width: tagViewW, height: labelH)
            
            label.sizeToFit()
            
            tagView.addSubview(label)
            
            label.center = tagView.middlePoint
            
            scrollView.addSubview(tagView)
            
            titleLabels.append(label)
            
            tagView.userInteractionEnabled = true
            let tapGes                   = UITapGestureRecognizer(target: self, action: #selector(self.titleLabelClick(_:)))
            
            tagView.addGestureRecognizer(tapGes)
        }
    }
    
    func setupBottomLineAndScrollLine() {

        guard let initLabel = titleLabels.first else{
            return
        }
        
        initLabel.textColor = UIColor(hex6: labelBlackColor, alpha: labelFirBlackAlpha)
        
        addSubview(scrollLine)
        
        self.scrolLineY     = frame.height - titelBottomMargin - lineH
        
        scrollLine.frame           = CGRect(x: initLabel.frame.origin.x, y: frame.height - titelBottomMargin - lineH, width: initLabel.width, height: lineH)
        
    }
}

// MARK:- 监听Label的点击
extension CMDBPageTitleView {
    @objc func titleLabelClick(tapGes : UITapGestureRecognizer) {
        
        let currentView  = tapGes.view
        
        let currentLabel = titleLabels[(currentView?.tag)!]
        
        
        if currentIndex == (currentView?.tag)! {
            return
        }
        
        let previousLabel       = titleLabels[currentIndex]
        
        currentLabel.textColor  = word87Color
        
        previousLabel.textColor = word54Color
        
        currentIndex            = (tapGes.view?.tag)!
        
        //滚动条
        let scrollLineX         = currentLabel.frame.origin.x  + CGFloat(currentIndex) * tagViewW
        
        UIView.animateWithDuration(0.3) {
            self.scrollLine.frame.origin.x = scrollLineX
            self.scrollLine.frame.size.width = currentLabel.width
        }
        
        // delegate
        delegate?.pageTitleView(self, selectedIndex: currentIndex)
    }
}

extension CMDBPageTitleView{
    
    func setTitleWithProgress(progress : CGFloat, sourceIndex : Int, targetIndex : Int) {

        
        let sourceLabel = titleLabels[sourceIndex]
        
        let targetLabel = titleLabels[targetIndex]
        
        tagViewW             = CMDB_Screen_Width / CGFloat(titles.count)
        
        let moveTotalX = targetLabel.frame.origin.x + CGFloat(targetIndex) * tagViewW - CGFloat(sourceIndex) * tagViewW - sourceLabel.frame.origin.x

        let moveX = moveTotalX * progress

        let scrollLineX         = moveX + sourceLabel.frame.origin.x + CGFloat(sourceIndex) * tagViewW

        self.scrollLine.frame.origin.x = scrollLineX

        self.scrollLine.frame.size.width = targetLabel.width

        targetLabel.textColor   = word87Color

        sourceLabel.textColor   = word54Color

        currentIndex = targetIndex

    }
}

